<%@ Page Language="C#" meta:resourcekey="Page" Inherits="SalarSoft.ASProxy.PageInMasterLocale" %>

<%@ Import Namespace="SalarSoft.ASProxy.Exposed" %>
<%@ Import Namespace="SalarSoft.ASProxy" %>
<%@ Import Namespace="System.Threading" %>
<html>

<script runat="server">
    bool _HasError = true;
    string _ToDisplayUrl = "";
    string _ErrorMessage = "";
    string _ResponseContent = "";
    string _ExtraCodesForBody = "";
    string _ExtraCodesForPage = "";
    UserOptions _userOptions;

    protected void Page_Load(object sender, EventArgs e)
    {
        Consts.FilesConsts.PageDefault_Dynamic = System.IO.Path.GetFileName(Request.Url.AbsolutePath).ToLower();
        _userOptions = UserOptions.ReadFromRequest();

        if (Configurations.Authentication.Enabled)
        {
            if (!Configurations.Authentication.HasPermission(User.Identity.Name, Configurations.AuthenticationConfig.UserPermission.Pages))
            {
                _ErrorMessage = "Access denied!";
                _ErrorMessage += "<br />You do not have access to browse pages. Ask site administrator to grant permission.";
                _HasError = true;
                return;
            }
        }

        ProccessRequest();

        //HttpRequest rq = HttpContext.Current.Request;
        //HttpResponse rs = HttpContext.Current.Response;
        //string redirurl = "HttpContext_Next.aspx";
        //HttpContext.Current.RewritePath(redirurl);			

        //HttpContext myContext = HttpContext.Current;
        //Rewrite the internal path            
        //myContext.RewritePath("HttpContext_Next.aspx"); 
        //Response.RedirectLocation = "HttpContext_Next.aspx";
    }

    void ProccessRequest()
    {
        if (UrlProvider.IsASProxyAddressUrlIncluded(Request.QueryString))
        {
            using (IEngine engine = (IEngine)Providers.GetProvider(ProviderType.IEngine))
            {

                engine.UserOptions = _userOptions;
                engine.DataTypeToProcess = DataTypeToProcess.Html;
                engine.RequestInfo.SetContentType(MimeContentType.text_html);

                engine.Initialize(Request);
                engine.RequestInfo.RequestUrl = UrlProvider.CorrectInputUrl(engine.RequestInfo.RequestUrl);

                GetResults(engine);
            }
        }
    }

    void GetResults(IEngine engine)
    {
        _ToDisplayUrl = engine.RequestInfo.RequestUrl;
        try
        {
            MimeContentType pageContentType;
            if (!UrlProvider.IsFTPUrl(engine.RequestInfo.RequestUrl))
            {
                engine.ExecuteHandshake();

                if (engine.LastStatus == LastStatus.Error)
                {
                    if (Systems.LogSystem.ErrorLogEnabled)
                        Systems.LogSystem.LogError(engine.LastErrorMessage, engine.RequestInfo.RequestUrl);

                    _ErrorMessage = engine.LastErrorMessage;
                    if (string.IsNullOrEmpty(_ErrorMessage))
                        _ErrorMessage = "Unknown error on requesting data";
                    _HasError = true;
                    return;
                }
                pageContentType = engine.ResponseInfo.ContentTypeMime;
            }
            else
                pageContentType = MimeContentType.application;

            switch (pageContentType)
            {
                case MimeContentType.application:
                    if (WebMethods.IsMethod(engine.RequestInfo.RequestMethod, WebMethods.DefaultMethods.GET))
                        Response.Redirect(UrlProvider.GetASProxyPageUrl(Consts.FilesConsts.PageDownload, engine.RequestInfo.RequestUrl, _userOptions.EncodeUrl), false);
                    else
                    {
                        string postUrl = UrlBuilder.AppendAntoherQueries(engine.RequestInfo.RequestUrl, engine.RequestInfo.PostDataString);
                        Response.Redirect(UrlProvider.GetASProxyPageUrl(Consts.FilesConsts.PageDownload, postUrl, _userOptions.EncodeUrl), false);
                    }
                    return;
                case MimeContentType.image_gif:
                case MimeContentType.image_jpeg:
                    _ResponseContent = HtmlTags.ImgTag(UrlProvider.GetASProxyPageUrl(Consts.FilesConsts.PageImages, engine.RequestInfo.RequestUrl, _userOptions.EncodeUrl));
                    return;
            }

            // Don't process for these type in default page
            if (pageContentType == MimeContentType.text_css || pageContentType == MimeContentType.text_plain || pageContentType == MimeContentType.text_javascript)
                engine.DataTypeToProcess = DataTypeToProcess.None;

            // apply http compression
            SalarSoft.ASProxy.BuiltIn.HttpCompressor.ApplyCompression(engine.ResponseInfo.ContentTypeMime);

            // Execute the request
            _ResponseContent = engine.ExecuteToString();


            // Set response query
            _ToDisplayUrl = engine.ResponseInfo.ResponseUrl;

            // If content is text format
            if (pageContentType == MimeContentType.text_css || pageContentType == MimeContentType.text_plain || pageContentType == MimeContentType.text_javascript)
                _ResponseContent = "<pre>" + HttpUtility.HtmlEncode(_ResponseContent) + "</pre>";

            // Response charset
            Response.ContentEncoding = engine.ResponseInfo.ContentEncoding;
            Response.Charset = engine.ResponseInfo.ContentEncoding.BodyName;

            if (engine.LastStatus == LastStatus.Error)
            {
                if (Systems.LogSystem.ErrorLogEnabled)
                    Systems.LogSystem.LogError(engine.LastErrorMessage, engine.RequestInfo.RequestUrl);

                _ErrorMessage = engine.LastErrorMessage;
                _HasError = true;
                _ResponseContent = "";
                return;
            }
            else if (engine.LastStatus == LastStatus.ContinueWithError)
            {
                if (Systems.LogSystem.ErrorLogEnabled)
                    Systems.LogSystem.LogError(engine.LastErrorMessage, engine.RequestInfo.RequestUrl);

                // if has error but can continue
                _ErrorMessage = engine.LastErrorMessage;
                _HasError = true;
            }


            if (engine.UserOptions.DocType || !engine.ResponseInfo.HtmlIsFrameSet)
            {
                Response.Write(engine.ResponseInfo.HtmlDocType);
            }
            //Response.Write(engine.ResponseInfo.ExtraCodesForPage);
            _ExtraCodesForPage = engine.ResponseInfo.ExtraCodesForPage;
            _ExtraCodesForBody = engine.ResponseInfo.ExtraCodesForBody;

            if (engine.UserOptions.PageTitle)
            {
                Page.Title = engine.ResponseInfo.HtmlPageTitle + " - [" + Consts.General.ASProxyName + "]";
            }
            if (engine.ResponseInfo.HtmlIsFrameSet)
            {
                ApplyFramesetResults(engine);
                return;
            }
        }
        catch (ThreadAbortException) { }
        catch (Exception ex)
        {
            if (Systems.LogSystem.ErrorLogEnabled)
                Systems.LogSystem.LogError(ex, ex.Message, engine.RequestInfo.RequestUrl);

            _ErrorMessage = ex.Message;
            _HasError = true;
        }
    }

    void ApplyFramesetResults(IEngine resutls)
    {
        if (resutls.ResponseInfo.HtmlIsFrameSet)
        {
            string result = "";
            if (WebMethods.IsMethod(resutls.RequestInfo.RequestMethod, WebMethods.DefaultMethods.GET))
                result = UrlProvider.GetASProxyPageUrl(Consts.FilesConsts.PageDirectHtml, resutls.RequestInfo.RequestUrl, true);
            else
                result = UrlBuilder.AppendAntoherQueries(Consts.FilesConsts.PageDirectHtml, resutls.RequestInfo.PostDataString);
            _ResponseContent = HtmlTags.IFrameTag(result, "100%", "600%");
        }
        // quit
        HttpContext.Current.ApplicationInstance.CompleteRequest();
    }

    string FloatDirection(bool inPageDirection)
    {
        if (inPageDirection)
            return Resources.Languages.TextAlign;
        else
        {
            if (Resources.Languages.TextAlign.ToLower() == "left")
                return "right";
            else
                return "left";
        }
    }
</script>

<head id="Head1" runat="server">
    <title id="Title1" runat="server">Surf the web with ASProxy</title>

    <script src="scripts/base64encoder.js" type="text/javascript" asproxydone="2"></script>

    <!-- Surf the web invisibly using ASProxy power. A Powerfull web proxy is in your hands. -->
    <link rel="Stylesheet" href="theme/style/surfstyle.css" type="text/css" asproxydone="2" />
    <style type="text/css">
        @font-face
        {
            font-family: Arial;
            font-style: normal;
            font-weight: normal;
            src: url("Font/Durga.eot");
        }
        @font-face
        {
            font-family: Arial;
            font-style: normal;
            font-weight: normal;
            src: url("Font/Durga.ttf");
        }
        .bengali
        {
            font-family: Arial;
            font-weight: bold;
            font-size: 25pt;
        }
        p.normal
        {
            font-style: normal;
            font-size: 20pt;
        }
        html, body {
            font-family: Arial !important;
            font-weight:normal !important;
            font-size: 20pt !important;
        }
        font{
            font-family: Arial !important;
            font-weight:normal !important;
            font-size: 20pt !important;
            }
    </style>
</head>
<body>

    <script asproxydone="2" type="text/javascript">
        var _ASProxyVersion = "<%=Consts.General.ASProxyVersion %>";
        function toggleOpt(lnk) {
            var trMoreOpt = document.getElementById('MoreOpts');
            if (trMoreOpt.style.display == 'none') {
                trMoreOpt.style.display = '';
                //lnk.innerHTML = '<%=this.GetLocalResourceObject("lnkMoreOptions")%>';
            } else {
                trMoreOpt.style.display = 'none';
                //lnk.innerHTML = '<%=this.GetLocalResourceObject("lnkMoreOptions")%>';
            }
        }
    </script>

    <script asproxydone="2" src="scripts/surfoptions.js" type="text/javascript"></script>

    <script asproxydone="2" type="text/javascript">
var _Page_B64Unknowner = "<%=Consts.Query.Base64Unknowner%>";
var _Page_CookieName = "<%=Consts.FrontEndPresentation.UserOptionsCookieName%>";
var _Page_UrlIsEmpty = '<%=this.GetLocalResourceObject("UrlIsEmpty")%>';
var _Page_XNav = '<%=Consts.FilesConsts.PageDefault_Dynamic%>';
function _Page_SetOptions(){
_XPage.Translatetion.checked =<%=_userOptions.Translatetion.ToString().ToLower() %>;
_XPage.ProcessLinks.checked =<%=_userOptions.Links.ToString().ToLower() %>;
_XPage.DisplayImages.checked =<%=_userOptions.Images.ToString().ToLower() %>;
_XPage.Forms.checked =<%=_userOptions.SubmitForms.ToString().ToLower() %>;
_XPage.Compression.checked =<%=_userOptions.HttpCompression.ToString().ToLower() %>;
_XPage.ImgCompressor.checked =<%=_userOptions.ImageCompressor.ToString().ToLower() %>;
_XPage.Cookies.checked =<%=_userOptions.Cookies.ToString().ToLower() %>;
_XPage.TempCookies.checked =<%=_userOptions.TempCookies.ToString().ToLower() %>;
_XPage.OrginalUrl.checked =<%=_userOptions.OrginalUrl.ToString().ToLower() %>;
_XPage.Frames.checked =<%=_userOptions.Frames.ToString().ToLower() %>;
_XPage.PageTitle.checked =<%=_userOptions.PageTitle.ToString().ToLower() %>;
_XPage.UTF8.checked =<%=_userOptions.ForceEncoding.ToString().ToLower() %>;
_XPage.RemoveScripts.checked =<%=_userOptions.RemoveScripts.ToString().ToLower() %>;
_XPage.RemoveObjects.checked =<%=_userOptions.RemoveObjects.ToString().ToLower() %>;
_XPage.EncodeUrl.checked=<%=_userOptions.EncodeUrl.ToString().ToLower() %>;
}
    </script>
<%=_ExtraCodesForPage%>
    <div id="__ASProxyContainer" style="float: left; width: 100%; height: auto;">
        <%=_ExtraCodesForBody%>
        <%=_ResponseContent%>
    </div>
    <div id="ASProxyFormBlock" class="ASPXBlock" style="position: fixed;width:98yyyyyy%" dir="<%=Resources.Languages.TextDirection%>">
        <div class="ASPXMain" style="text-align: <%=Resources.Languages.TextAlign%>; float: left;
            width: 99.4%">
            <div class="AddrBar" style="float: left; width: 100%;">
                <div style="float: left; width: 90%;">
                    <input name="url" type="text" id="txtUrl" dir="ltr" style="width: 90% !important;"
                        class="TextBox" onkeyup="_Page_HandleTextKey(event)" value="<%=HttpUtility.HtmlEncode(_ToDisplayUrl)%>" />
                    <input type="button" onclick="_Page_SubmitForm()" value="<%=this.GetLocalResourceObject("btnDisplay")%>"
                        id="btnASProxyDisplayButton" class="Button" />
                </div>
                <div style="float: right; width: 10%;">
                    <div style="float: left; padding-right: 5px;">
                        <a asproxydone="2" id="lnkMoreOpt" href="javascript:void(0);" onclick="toggleOpt(this);">
                            <%=this.GetLocalResourceObject("lnkMoreOptions")%></a>
                    </div>
                    <div style="float: right;">
                        <a href="javascript:void(0);" onclick="javascript:_Page_HideASProxyBlock();" asproxydone="2"
                            title="Close this bar"><span style="font-size: smaller; float: <%=FloatDirection(false)%>">
                                (X)</span></a>
                    </div>
                </div>
            </div>
            <div style="float: left; width: 100%;">
                <div id="MoreOpts" style="display: none; width: 100%">
                    <div style="float: left; width: 100%">
                        <a href="cookieman.aspx" target="_blank" asproxydone="2" style="padding-right: 10px;">
                            <%=this.GetLocalResourceObject("CookieManager")%></a> <a href="download.aspx" target="_blank"
                                asproxydone="2">
                                <%=this.GetLocalResourceObject("DownloadTool")%></a>
                    </div>
                    <%if (Configurations.UserOptions.Translatetion.Changeable)
                      { %><div class="ASPXOpt" style="width: 130px !important; float: left; text-align: left;">
                      <input id="chkTranslatetion" type="checkbox" onclick="_Page_SaveOptions()" /><label
                          for="chkTranslatetion"><%=this.GetLocalResourceObject("chkTranslatetion")%></label></div>
                    <%} %>
                    <%if (Configurations.UserOptions.ForceEncoding.Changeable)
                      { %><div class="ASPXOpt" style="width: 130px !important; float: left; text-align: left;">
                      <input id="chkUTF8" type="checkbox" onclick="_Page_SaveOptions()" /><label for="chkUTF8"><%=this.GetLocalResourceObject("chkUTF8")%></label></div>
                    <%} %>
                    <%if (Configurations.UserOptions.OrginalUrl.Changeable)
                      { %><div class="ASPXOpt" style="width: 130px !important; float: left; text-align: left;">
                      <input id="chkOrginalUrl" type="checkbox" checked="checked" onclick="_Page_SaveOptions()" /><label
                          for="chkOrginalUrl"><%=this.GetLocalResourceObject("chkOrginalUrl")%></label></div>
                    <%} %>
                    <%if (Configurations.UserOptions.RemoveScripts.Changeable)
                      { %><div class="ASPXOpt" style="width: 130px !important; float: left; text-align: left;">
                      <input id="chkRemoveScripts" type="checkbox" onclick="_Page_SaveOptions()" /><label
                          for="chkRemoveScripts"><%=this.GetLocalResourceObject("chkRemoveScripts")%></label></div>
                    <%} %>
                    <%if (Configurations.UserOptions.Cookies.Changeable)
                      { %><div class="ASPXOpt" style="width: 130px !important; float: left; text-align: left;">
                      <input id="chkCookies" type="checkbox" checked="checked" onclick="_Page_SaveOptions()" /><label
                          for="chkCookies"><%=this.GetLocalResourceObject("chkCookies")%></label></div>
                    <%} %>
                    <%if (Configurations.UserOptions.HttpCompression.Changeable)
                      { %><div class="ASPXOpt" style="width: 130px !important; float: left; text-align: left;">
                      <input id="chkCompression" type="checkbox" onclick="_Page_SaveOptions()" /><label
                          for="chkCompression"><%=this.GetLocalResourceObject("chkCompression")%></label></div>
                    <%} %>
                    <%if (Configurations.UserOptions.ImageCompressor.Changeable && Configurations.ImageCompressor.Enabled)
                      { %><div class="ASPXOpt" style="width: 130px !important; float: left; text-align: left;">
                      <input id="chkImgCompressor" type="checkbox" onclick="_Page_SaveOptions()" /><label
                          for="chkImgCompressor"><%=this.GetLocalResourceObject("chkImgCompressor")%></label></div>
                    <%} %>
                    <%if (Configurations.UserOptions.EncodeUrl.Changeable)
                      { %><div class="ASPXOpt" style="width: 130px !important; float: left; text-align: left;">
                      <input id="chkEncodeUrl" type="checkbox" checked="checked" onclick="_Page_SaveOptions()" /><label
                          for="chkEncodeUrl"><%=this.GetLocalResourceObject("chkEncodeUrl")%></label></div>
                    <%} %>
                    <%if (Configurations.UserOptions.Frames.Changeable)
                      { %><div class="ASPXOpt" style="width: 130px !important; float: left; text-align: left;">
                      <input id="chkFrames" type="checkbox" checked="checked" onclick="_Page_SaveOptions()" /><label
                          for="chkFrames"><%=this.GetLocalResourceObject("chkFrames")%></label></div>
                    <%} %>
                    <%if (Configurations.UserOptions.PageTitle.Changeable)
                      { %><div class="ASPXOpt" style="width: 130px !important; float: left; text-align: left;">
                      <input id="chkPageTitle" type="checkbox" checked="checked" onclick="_Page_SaveOptions()" /><label
                          for="chkPageTitle"><%=this.GetLocalResourceObject("chkPageTitle")%></label></div>
                    <%} %>
                    <%if (Configurations.UserOptions.SubmitForms.Changeable)
                      { %><div class="ASPXOpt" style="width: 130px !important; float: left; text-align: left;">
                      <input id="chkForms" type="checkbox" checked="checked" onclick="_Page_SaveOptions()" /><label
                          for="chkForms"><%=this.GetLocalResourceObject("chkForms")%></label></div>
                    <%} %>
                    <%if (Configurations.UserOptions.RemoveObjects.Changeable)
                      { %><div class="ASPXOpt" style="width: 130px !important; float: left; text-align: left;">
                      <input id="chkRemoveObjects" type="checkbox" checked="checked" onclick="_Page_SaveOptions()" /><label
                          for="chkRemoveObjects"><%=this.GetLocalResourceObject("chkRemoveObjects")%></label></div>
                    <%} %>
                    <%if (Configurations.UserOptions.Links.Changeable)
                      { %><div class="ASPXOpt" style="width: 130px !important; float: left; text-align: left;">
                      <input id="chkProcessLinks" type="checkbox" checked="checked" onclick="_Page_SaveOptions()" /><label
                          for="chkProcessLinks"><%=this.GetLocalResourceObject("chkProcessLinks")%></label></div>
                    <%} %>
                    <%if (Configurations.UserOptions.Images.Changeable)
                      { %><div class="ASPXOpt" style="width: 130px !important; float: left; text-align: left;">
                      <input id="chkDisplayImages" type="checkbox" checked="checked" onclick="_Page_SaveOptions()" /><label
                          for="chkDisplayImages"><%=this.GetLocalResourceObject("chkDisplayImages")%></label></div>
                    <%} %>
                    <%if (Configurations.UserOptions.TempCookies.Changeable)
                      { %><div class="ASPXOpt" style="width: 130px !important; float: left; text-align: left;">
                      <input id="chkTempCookies" type="checkbox" checked="checked" onclick="_Page_SaveOptions()" /><label
                          for="chkTempCookies"><%=this.GetLocalResourceObject("chkTempCookies")%></label></div>
                    <%} %>
                </div>
            </div>
        </div>
        
        <script type="text/javascript" asproxydone="2">
            _Page_Initialize();
            _Page_SetOptions();
            // iframed, hide the form
            if (window.self != window.top) _Page_HideASProxyBlock();
        </script>

        <%if (_HasError)
          { %>
        <div title="Error message" style="text-align: center; color: Red; font-weight: bold;
            font-family: Tahoma; font-size: 10pt;">
            <%=_ErrorMessage%></div>
        <%} %>
        <noscript style="color: Maroon; font-weight: bold; font-family: Tahoma; font-size: 10pt;">
            <%=this.GetLocalResourceObject("JsIsDisabled")%></noscript>
    </div>
</body>
</html>
